<?php
namespace Modules\Wachat\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Wachat\Entities\Weixin;
use Modules\Wachat\Http\Requests\WeixinRequest;
class WeixinController extends Controller
{
    //显示列表
    public function index()
    {
        $data = Weixin::paginate(10);
        return view('wachat::weixin.index', compact('data'));
    }

    //创建视图
    public function create(Weixin $weixin)
    {
        $data = $weixin->pluck('value','name');
        return view('wachat::weixin.create',compact('data'));
    }

    //保存数据
    public function store(WeixinRequest $request,Weixin $weixin)
    {
        //截断数据
        $weixin->truncate();
        //循环存入数据
        foreach ($request->all() as $name => $value){
            //不保存_token值
            if ($name != '_token'){
                //保存前端存入的内容
                $weixin->create(
                    [
                        'name' => $name,
                        'value' => $value,
                    ]
                );
            }
        }
        return redirect('/wachat/weixin')->with('success', '保存成功');
    }

    //显示记录
    public function show(Weixin $field)
    {
        return view('wachat::weixin.show', compact('field'));
    }

    //编辑视图
    public function edit(Weixin $weixin)
    {
        return view('wachat::weixin.edit', compact('weixin'));
    }

    //更新数据
    public function update(WeixinRequest $request, Weixin $weixin)
    {
        $weixin->update($request->all());
        return redirect('/wachat/weixin')->with('success','更新成功');
    }

    //删除模型
    public function destroy(Weixin $weixin)
    {
        $weixin->delete();
        return redirect('wachat/weixin')->with('success','删除成功');
    }
}
